<!DOCTYPE html>
<html lang="zh-CN">
    <head>
    <meta charset="utf-8">

    

    <!-- 渲染优化 -->
    <meta name="renderer" content="webkit">
    <meta name="force-rendering" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="HandheldFriendly" content="True" >
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <!--icon-->

    
        <link rel="shortcut icon" href="https://s1.imgpp.com/2022/01/28/owl.png">
    
    
    
    
    


    <!-- meta -->


<title>js函数 | 猫头鹰的BLOG</title>


    <meta name="keywords" content="js, js函数, call, apply, 猫头鹰的博客">




    <!-- OpenGraph -->
 
    <meta name="description" content="数据类型分类 基本类型 string:任意字符串 number:任意数字 boolean:true&#x2F;false undefined:undefined null:null   对象(引用)类型 Object:任意对象(无序) Function:一种特别的对象(可执行） Array:一种特别的对象(数值下标，有序)    判断 typeof：返回数据类型的字符串表达(首字母会变小写),可以判断und">
<meta property="og:type" content="article">
<meta property="og:title" content="js函数">
<meta property="og:url" content="http://example.com/2022/03/03/JavaScript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="猫头鹰的BLOG">
<meta property="og:description" content="数据类型分类 基本类型 string:任意字符串 number:任意数字 boolean:true&#x2F;false undefined:undefined null:null   对象(引用)类型 Object:任意对象(无序) Function:一种特别的对象(可执行） Array:一种特别的对象(数值下标，有序)    判断 typeof：返回数据类型的字符串表达(首字母会变小写),可以判断und">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://images.pexels.com/photos/6281163/pexels-photo-6281163.jpeg?cs=srgb&dl=pexels-yaroslav-shuraev-6281163.jpg&fm=jpg">
<meta property="article:published_time" content="2022-03-03T02:00:20.000Z">
<meta property="article:modified_time" content="2022-05-30T15:03:00.606Z">
<meta property="article:author" content="张程">
<meta property="article:tag" content="js">
<meta property="article:tag" content="js函数">
<meta property="article:tag" content="call">
<meta property="article:tag" content="apply">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://images.pexels.com/photos/6281163/pexels-photo-6281163.jpeg?cs=srgb&dl=pexels-yaroslav-shuraev-6281163.jpg&fm=jpg">


    
<link rel="stylesheet" href="/css/style/main.css">
 

    
    
    
        <link rel="stylesheet" id="hl-default-theme" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.2/styles/arduino-light.min.css" media="none" >
        
            <link rel="stylesheet" id="hl-dark-theme" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.2/styles/obsidian.min.css" media="none">
        
    

    

    
    
<link rel="stylesheet" href="/css/style/dark.css">

    
<script src="/js/darkmode.js"></script>



     
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
 

    <!-- custom head -->

<meta name="generator" content="Hexo 6.0.0"></head>

    <body>
        <div id="app">
            <header class="header">
    <div class="header__left">
        <a href="/" class="button">
            <span class="logo__text">猫头鹰</span>
        </a>
    </div>
    <div class="header__right">
        
            <div class="navbar__menus">
                
                    <a href="/" class="navbar-menu button">首页</a>
                
                    <a href="/tags/" class="navbar-menu button">标签</a>
                
                    <a href="/categories/" class="navbar-menu button">分类</a>
                
                    <a href="/archives/" class="navbar-menu button">归档</a>
                
            </div>
        
        
        
    <a href="/search/" id="btn-search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" width="24" height="24" fill="currentColor" stroke="currentColor" stroke-width="32"><path d="M192 448c0-141.152 114.848-256 256-256s256 114.848 256 256-114.848 256-256 256-256-114.848-256-256z m710.624 409.376l-206.88-206.88A318.784 318.784 0 0 0 768 448c0-176.736-143.264-320-320-320S128 271.264 128 448s143.264 320 320 320a318.784 318.784 0 0 0 202.496-72.256l206.88 206.88 45.248-45.248z"></path></svg>
    </a>


        
        
    <a href="javaScript:void(0);" id="btn-toggle-dark">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
    </a>


        
            <a class="dropdown-icon button" id="btn-dropdown" tabindex="0"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width='24' height='24' fill="none" stroke="currentColor" stroke-width="0.7" stroke-linecap="round" stroke-linejoin="round"><path fill="currentColor" d="M3.314,4.8h13.372c0.41,0,0.743-0.333,0.743-0.743c0-0.41-0.333-0.743-0.743-0.743H3.314c-0.41,0-0.743,0.333-0.743,0.743C2.571,4.467,2.904,4.8,3.314,4.8z M16.686,15.2H3.314c-0.41,0-0.743,0.333-0.743,0.743s0.333,0.743,0.743,0.743h13.372c0.41,0,0.743-0.333,0.743-0.743S17.096,15.2,16.686,15.2z M16.686,9.257H3.314c-0.41,0-0.743,0.333-0.743,0.743s0.333,0.743,0.743,0.743h13.372c0.41,0,0.743-0.333,0.743-0.743S17.096,9.257,16.686,9.257z"></path></svg></a>
            <div class="dropdown-menus" id="dropdown-menus">
                
                    <a href="/" class="dropdown-menu button">首页</a>
                
                    <a href="/tags/" class="dropdown-menu button">标签</a>
                
                    <a href="/categories/" class="dropdown-menu button">分类</a>
                
                    <a href="/archives/" class="dropdown-menu button">归档</a>
                
            </div>
        
    </div>
</header>


            <main class="main">
    

<div class="post-title">
    <h1 class="post-title__text">
        js函数
    </h1>
    <div class="post-title__meta">
        <a href="/archives/2022/03/" class="post-meta__date button">2022-03-03</a>
        
    <span class="separate-dot"></span><a href="/categories/javascript%E9%AB%98%E7%BA%A7/" class="button">javascript高级</a>

 
        
    
     
    <span id="busuanzi_container_page_pv" hidden>
        <span class="separate-dot"></span>
        <span></span>
        <span id="busuanzi_value_page_pv"></span>
        <span>次观看</span>
    </span>



 

 
    </div>
</div>



<article class="post content-card">
    <div class="post__header">
    
        <div class="post-thumbnail lazy" data-bg="https://images.pexels.com/photos/6281163/pexels-photo-6281163.jpeg?cs=srgb&amp;dl=pexels-yaroslav-shuraev-6281163.jpg&amp;fm=jpg"></div>
    
</div>
    <div class="post__content">
        <h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><h4 id="分类"><a href="#分类" class="headerlink" title="分类"></a>分类</h4><ol>
<li>基本类型<ol>
<li>string:任意字符串</li>
<li>number:任意数字</li>
<li>boolean:true/false</li>
<li>undefined:undefined</li>
<li>null:null</li>
</ol>
</li>
<li>对象(引用)类型<ol>
<li>Object:任意对象(无序)</li>
<li>Function:一种特别的对象(可执行）</li>
<li>Array:一种特别的对象(数值下标，有序)</li>
</ol>
</li>
</ol>
<h4 id="判断"><a href="#判断" class="headerlink" title="判断"></a>判断</h4><ol>
<li><p>typeof：返回数据类型的字符串表达(首字母会变小写),可以判断undefined/数值/字符串/布尔值</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> a);

<span class="hljs-keyword">var</span> a=<span class="hljs-number">1</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> a);

<span class="hljs-keyword">var</span> a=<span class="hljs-string">&quot;zs&quot;</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> a);

<span class="hljs-keyword">var</span> a=<span class="hljs-literal">true</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> a);

<span class="hljs-keyword">var</span> a=<span class="hljs-literal">null</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> a);</code></pre>
<p><img src="https://imgpp.com/s1/2022/02/24/image1fd4b6773d2d2d4d.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/02/24/image1fd4b6773d2d2d4d.png" srcset="" alt="结果图"></p>
</li>
<li><p>instanceof：返回一个布尔值，可以判断对象类型的具体类型</p>
<pre><code class="hljs javascript">a <span class="hljs-keyword">instanceof</span> b <span class="hljs-comment">//a是不是b的实例</span>

<span class="hljs-keyword">var</span> b1=&#123;

 <span class="hljs-attr">b2</span>:[<span class="hljs-number">1</span>,<span class="hljs-string">&quot;abc&quot;</span>,<span class="hljs-built_in">console</span>.log],

 <span class="hljs-attr">b3</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;

 <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;b3&quot;</span>);

 &#125;

&#125;

<span class="hljs-built_in">console</span>.log(b1 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Object</span>);

<span class="hljs-built_in">console</span>.log(b1.b2 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Array</span>,b1.b2 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Object</span>);

<span class="hljs-built_in">console</span>.log(b1.b3 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Function</span>,b1.b3 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Object</span>);</code></pre>
<p><img src="https://imgpp.com/s1/2022/02/24/imageb807432c6a932a6b.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/02/24/imageb807432c6a932a6b.png" srcset="" alt="结果图"></p>
</li>
<li><p>===</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a=<span class="hljs-number">1</span>
<span class="hljs-keyword">typeof</span> a===<span class="hljs-string">&quot;number&quot;</span>
a <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Number</span></code></pre></li>
</ol>
<h2 id="数据，变量和内存"><a href="#数据，变量和内存" class="headerlink" title="数据，变量和内存"></a>数据，变量和内存</h2><h4 id="数据"><a href="#数据" class="headerlink" title="数据"></a>数据</h4><blockquote>
<p>储存在内存中，代表特殊信息 本质0101…<br>特点：可传递，可运算（算术/逻辑/连接/赋值/运行函数）<br>一切皆数据</p>
</blockquote>
<h4 id="内存"><a href="#内存" class="headerlink" title="内存"></a>内存</h4><blockquote>
<ol>
<li>基本数据类型保存在栈内存中</li>
<li>引用数据类型保存在堆内存中<br><img src="https://imgpp.com/s1/2022/02/25/image.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/02/25/image.png" srcset="" alt="图片"></li>
</ol>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-comment">// 基本数据类型</span>
<span class="hljs-keyword">var</span> a=<span class="hljs-number">1</span>
b=a
a=<span class="hljs-number">2</span>
<span class="hljs-built_in">console</span>.log(b);

<span class="hljs-comment">// 引用数据类型</span>
<span class="hljs-keyword">var</span> obj1=&#123;<span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;张三&#x27;</span>&#125;
obj2=obj1
obj1.name=<span class="hljs-string">&quot;李四&quot;</span>
<span class="hljs-built_in">console</span>.log(obj2);</code></pre>
<p><img src="https://imgpp.com/s1/2022/02/25/image9e4733ae51acd686.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/02/25/image9e4733ae51acd686.png" srcset="" alt="图片"></p>
<h4 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h4><blockquote>
<p>分变量名和变量值<br>变量名用来查找对应的内存<br>变量值就是内存中保存的数据<br><img src="https://imgpp.com/s1/2022/02/25/image26680d1dff621f16.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/02/25/image26680d1dff621f16.png" srcset="" alt="图片"></p>
</blockquote>
<h4 id="三者关系"><a href="#三者关系" class="headerlink" title="三者关系"></a>三者关系</h4><blockquote>
<p>内存是存储数据的空间<br>变量是内存的标识<br>栈内存：全局变量和局部变量<br>堆内存：对象</p>
</blockquote>
<blockquote>
<p>两个引用变量指向同一个对象，<br>其中一个指向另一个对象，<br>另一个引用变量还指向前一个对象</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a=&#123;<span class="hljs-attr">age</span>:<span class="hljs-number">12</span>&#125;

<span class="hljs-keyword">var</span> b=a

a=&#123;<span class="hljs-attr">age</span>:<span class="hljs-number">13</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;bob&#x27;</span>&#125;

<span class="hljs-built_in">console</span>.log(b.age,a.name,a.age);</code></pre>
<blockquote>
<p>js调用函数时传递变量参数，是值传递还是引用传递?<br>都是值（基本/地址）传递</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn3</span>(<span class="hljs-params">obj</span>)</span>&#123;

 <span class="hljs-built_in">console</span>.log(obj.name);

&#125;

<span class="hljs-keyword">var</span> obj3=&#123;<span class="hljs-attr">name</span>:<span class="hljs-number">18</span>&#125;

<span class="hljs-keyword">var</span> obj4=obj3

obj4.name=<span class="hljs-number">19</span>

fn3(obj3)</code></pre>
<blockquote>
<p>js引擎如何管理内存？</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>)</span>&#123;
	<span class="hljs-keyword">var</span> b=&#123;&#125;	<span class="hljs-comment">//局部变量：函数执行完自动释放</span>
				<span class="hljs-comment">//对象：成为垃圾对象=》垃圾回收器回收</span>
&#125;
fn()</code></pre>

<h2 id="对象和函数"><a href="#对象和函数" class="headerlink" title="对象和函数"></a>对象和函数</h2><h4 id="函数各种形式"><a href="#函数各种形式" class="headerlink" title="函数各种形式"></a>函数各种形式</h4><pre><code class="hljs javascript"><span class="hljs-comment">// 对象中的函数</span>
<span class="hljs-attr">demo</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;
&#125;
<span class="hljs-comment">// 简写形式</span>
<span class="hljs-function"><span class="hljs-title">demo</span>(<span class="hljs-params"></span>)</span>&#123;
&#125;
<span class="hljs-comment">// js中，函数声明</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params"></span>)</span>&#123;
&#125;
<span class="hljs-comment">// 表达式</span>
demo=<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;
&#125;</code></pre>
<h4 id="调用对象属性两种写法"><a href="#调用对象属性两种写法" class="headerlink" title="调用对象属性两种写法"></a>调用对象属性两种写法</h4><pre><code class="hljs javascript">p.[<span class="hljs-string">&quot;xx&quot;</span>]	<span class="hljs-comment">//通用</span>
p.xx	<span class="hljs-comment">//特殊情况不能用</span></code></pre>
<blockquote>
<p>以下两种情况使用[“属性名”]<br>属性名包含特殊字符<br>变量名不确定</p>
</blockquote>
<h4 id="函数的调用"><a href="#函数的调用" class="headerlink" title="函数的调用"></a>函数的调用</h4><blockquote>
<p>1.demo()</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> fn2 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;fn2&quot;</span>);
&#125;
fn2()</code></pre>
<blockquote>
<p>2.obj.demo()</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> obj = &#123;
    <span class="hljs-function"><span class="hljs-title">demo</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;对象中的函数&quot;</span>);
    &#125;
&#125;
obj.demo()</code></pre>
<blockquote>
<p>fn.call(obj)    等同于fn成为obj的方法进行调用==obj.fn()<br>fn.apply(obj) 等同于fn成为obj的方法进行调用==obj.fn()</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> obj = &#123;
    <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;obj&quot;</span>,
    <span class="hljs-function"><span class="hljs-title">demo</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;对象中的函数&quot;</span>);
    &#125;
&#125;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn3</span>(<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.name);
&#125;
fn3.call(obj)
fn3.apply(obj)	<span class="hljs-comment">//结果：输出两个obj</span></code></pre>

<h4 id="IIFE"><a href="#IIFE" class="headerlink" title="IIFE"></a>IIFE</h4><blockquote>
<p>IIFE 立即调用函数表达式<br>(function)() 第一个括号把函数当成一个整体，第二个执行函数<br>隐藏实现（括号外不能看见）</p>
</blockquote>
<pre><code class="hljs javascript">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;IIFE:立即调用函数表达式&quot;</span>);
&#125;)()</code></pre>

<h4 id="函数的this"><a href="#函数的this" class="headerlink" title="函数的this"></a>函数的this</h4><blockquote>
<p>this从内向外找对象，若没有，this指向的就是window<br>任何函数都是通过某个对象调用的，若没有，就是window</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">color</span>) </span>&#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
    <span class="hljs-built_in">this</span>.color=color
    <span class="hljs-built_in">this</span>.getColor = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.color
    &#125;
    <span class="hljs-built_in">this</span>.setColor = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
        <span class="hljs-built_in">this</span>.color=color
    &#125;
&#125;</code></pre>
<pre><code class="hljs javscript">Person(&#x27;red&#x27;)	//this指向window

var p = new Person(&quot;green&quot;)	//this指向p这个实例化对象

p.getColor()	//this指向p这个对象

var obj = &#123;&#125;
p.setColor.call(obj, &quot;white&quot;)	//this指向obj这个对象
function fn() &#123;
    function fn2() &#123;
        console.log(this);
    &#125;
    fn2()
&#125;
fn()	//this指向window对象</code></pre>


<h2 id="遍历"><a href="#遍历" class="headerlink" title="遍历"></a>遍历</h2><h4 id="1-for"><a href="#1-for" class="headerlink" title="1.for()"></a>1.for()</h4><blockquote>
<p>普通遍历</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> arr=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>;i&lt;arr.length;i++)&#123;
    <span class="hljs-built_in">console</span>.log(arr[i]);
&#125;</code></pre>

<h4 id="2-forEach-el-index-gt"><a href="#2-forEach-el-index-gt" class="headerlink" title="2.forEach((el,index)=&gt;{})"></a>2.forEach((el,index)=&gt;{})</h4><blockquote>
<p>el:元素<br>index:索引</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> arr=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
arr.forEach(<span class="hljs-function">(<span class="hljs-params">el,index</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(el,index);
&#125;);</code></pre>

<h4 id="3-for-in"><a href="#3-for-in" class="headerlink" title="3.for in"></a>3.for in</h4><blockquote>
<p>index:索引</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> arr=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-keyword">for</span>(index <span class="hljs-keyword">in</span> arr)&#123;
    <span class="hljs-built_in">console</span>.log(arr[index]);
&#125;</code></pre>

<h4 id="4-for-of"><a href="#4-for-of" class="headerlink" title="4.for of"></a>4.for of</h4><blockquote>
<p>el:元素</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> arr=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-keyword">for</span>(el <span class="hljs-keyword">of</span> arr)&#123;
    <span class="hljs-built_in">console</span>.log(el);
&#125;</code></pre>

<h2 id="js高级函数"><a href="#js高级函数" class="headerlink" title="js高级函数"></a>js高级函数</h2><h4 id="call"><a href="#call" class="headerlink" title="call()"></a>call()</h4><blockquote>
<p>obj1.fn.call(obj)<br>一个对象的一个方法能通过call能调用另一个对象的属性和方法<br>本质：call能将对象作为参数传递</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> obj = &#123;
    <span class="hljs-attr">fullname</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">city</span>) </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.firstname +<span class="hljs-string">&quot;.&quot;</span>+<span class="hljs-built_in">this</span>.lastname+<span class="hljs-string">&quot;,我的城市是：&quot;</span>+city
    &#125;
&#125;
<span class="hljs-keyword">var</span> obj1 = &#123;
    <span class="hljs-attr">firstname</span>: <span class="hljs-string">&#x27;克罗斯迪亚诺&#x27;</span>,
    <span class="hljs-attr">lastname</span>:<span class="hljs-string">&#x27;罗纳尔多&#x27;</span>
&#125;
<span class="hljs-built_in">console</span>.log(obj.fullname.call(obj1,<span class="hljs-string">&#x27;里斯本&#x27;</span>));</code></pre>
<p><img src="https://imgpp.com/s1/2022/03/03/image50c5b316a050a8a0.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/03/03/image50c5b316a050a8a0.png" srcset="" alt="图片"></p>
<h4 id="apply"><a href="#apply" class="headerlink" title="apply()"></a>apply()</h4><blockquote>
<p>第一个参数:obj，函数this的指向，如果不传对象，可写为0<br>obj1.fn.apply(obj,[])<br>apply也能将对象作为参数传递<br>不同:apply还能将数组作为参数列表传递</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> obj = &#123;
    <span class="hljs-attr">fullname</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">nation,city</span>) </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.firstname +<span class="hljs-string">&quot;.&quot;</span>+<span class="hljs-built_in">this</span>.lastname+<span class="hljs-string">&quot;,国家：&quot;</span>+nation+<span class="hljs-string">&quot;,城市：&quot;</span>+city
    &#125;
&#125;
<span class="hljs-keyword">var</span> obj1 = &#123;
    <span class="hljs-attr">firstname</span>: <span class="hljs-string">&#x27;克罗斯迪亚诺&#x27;</span>,
    <span class="hljs-attr">lastname</span>:<span class="hljs-string">&#x27;罗纳尔多&#x27;</span>
&#125;
<span class="hljs-built_in">console</span>.log(obj.fullname.apply(obj1,[<span class="hljs-string">&#x27;葡萄牙&#x27;</span>,<span class="hljs-string">&#x27;里斯本&#x27;</span>]));</code></pre>
<p><img src="https://imgpp.com/s1/2022/03/03/image9dfbf4b61460722a.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/03/03/image9dfbf4b61460722a.png" srcset="" alt="图片"></p>
<h4 id="bind"><a href="#bind" class="headerlink" title="bind()"></a>bind()</h4><blockquote>
<p>fn.bind()方法:创造一个新的函数（闭包函数）<br>在bind()被调用时，bind的第一个参数为新函数this的指向，<br>其余函数为新函数的其他参数</p>
</blockquote>
<h2 id="arguments对象"><a href="#arguments对象" class="headerlink" title="arguments对象"></a>arguments对象</h2><blockquote>
<p>创建一个函数后自动生成的一个对象<br>具有索引，长度<br>但与数组不同，不具有数组的方法</p>
</blockquote>
<ol>
<li>将其转化为真实数组<pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;
<span class="hljs-keyword">var</span> arr=[...arguments]	<span class="hljs-comment">//...为拓展运算符</span>
&#125;</code></pre></li>
<li>将其转化为真实数组<pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params"></span>)</span>&#123;
	<span class="hljs-keyword">var</span> arr=<span class="hljs-built_in">Array</span>.from(<span class="hljs-built_in">arguments</span>)
&#125;</code></pre></li>
<li>将其转化为真实数组<pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params"></span>)</span>&#123;
	<span class="hljs-keyword">var</span> arr=<span class="hljs-built_in">Array</span>.prototype.slice.call(<span class="hljs-built_in">arguments</span>,<span class="hljs-number">0</span>)
	<span class="hljs-built_in">console</span>.log(arr,arr <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Array</span>)
&#125;
demo(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)</code></pre>
<blockquote>
<p>结果：[1,2,3],true</p>
</blockquote>
</li>
</ol>

    </div>
     
    <div class="post-footer__meta"><p>更新于 2022-05-30</p></div> 
    <div class="post-entry__tags"><a href="/tags/js/" class="post-tags__link button"># js</a><a href="/tags/js%E5%87%BD%E6%95%B0/" class="post-tags__link button"># js函数</a><a href="/tags/call/" class="post-tags__link button"># call</a><a href="/tags/apply/" class="post-tags__link button"># apply</a></div> 
</article>


    <div class="nav">
        <div class="nav__prev">
            
                <a href="/2022/03/07/%E5%AA%92%E4%BD%93%E6%9F%A5%E8%AF%A2/" class="nav__link">
                    <div>
                        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M589.088 790.624L310.464 512l278.624-278.624 45.248 45.248L400.96 512l233.376 233.376z" fill="#808080"></path></svg>
                    </div>
                    <div>
                        <div class="nav__label">
                            <!-- Previous Post -->
                            上一页
                        </div>
                        <div class="nav__title">
                            媒体查询
                        </div>
                    </div>
                </a>
            
        </div>
        <div class="nav__next">
            
                <a href="/2022/03/02/vue%E4%B8%AD%E6%93%8D%E4%BD%9C%E6%95%B0%E7%BB%84%E7%9A%84%E6%96%B9%E6%B3%95/" class="nav__link">
                    <div>
                        <div class="nav__label">
                            <!-- Next Post -->
                            下一页
                        </div>
                        <div class="nav__title">
                            vue中操作数组的方法
                        </div>
                    </div>
                    <div>
                        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M434.944 790.624l-45.248-45.248L623.04 512l-233.376-233.376 45.248-45.248L713.568 512z" fill="#808080"></path></svg>
                    </div>
                </a>
            
        </div>
    </div>



    <div class="post__comments content-card" id="comment">
        
    <h4>评论</h4>
    
    
    
    <div id="valine_container" class="valine_thread"></div>

    
    
    
    
    
    
    
    


    </div>



</main>

            <footer class="footer" style="font-size: 10px;">
     
    <a href="#" class="button" id="b2t" aria-label="回到顶部" title="回到顶部">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" width="32" height="32">
            <path d="M233.376 722.752L278.624 768 512 534.624 745.376 768l45.248-45.248L512 444.128zM192 352h640V288H192z" fill="currentColor"></path>
        </svg>
    </a>

    


    
    
    
        <span id="busuanzi_container_site_uv" hidden>
            <span>游客:</span>
            <span id="busuanzi_value_site_uv"></span>
            <span></span>
            
                <span> </span>
            
        </span>
    
    
        <span id="busuanzi_container_site_pv" hidden>
            <span>次数:</span>
            <span id="busuanzi_value_site_pv"></span>
            <span></span>
            
        </span>
    
 
 

 
    
    
    <p>
        <!-- Powered by <a href="https://hexo.io" target="_blank">Hexo</a> | Theme - <a href="https://github.com/ChrAlpha/hexo-theme-cards" target="_blank">Cards</a> -->
        <span style="color: black;">作者：张程</span>
    </p>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        var a_idx = 0;
            jQuery(document).ready(function($) {
                $("body").click(function(e) {
                    var a = new Array("会当凌绝顶，一览众山小", "春蚕到死丝方尽，蜡炬成灰泪始干", "夕阳无限好，只是近黄昏",
                     "位卑未敢忘忧国", "纸上得来终觉浅，绝知此事要躬行", "人生自古谁无死，留取丹心照汗青", "山重水复疑无路，柳暗花明又一村" ,
                     "小荷才露尖尖角，早有蜻蜓立上头", "床前明月光，疑是地上霜", "春色满园关不住，一枝红杏出墙来", 
                     "山际见来烟，竹中窥落日", "道狭草木长，夕露沾我衣","天长地久有时尽，此恨绵绵无绝期");
                    var color = new Array("#7B7B7B", "#CE0000", "#FF0080","#E800E8","#921AFF","#2828FF",
                    "#0072E3","#00CACA","#02DF82","#00DB00","#8CEA00","#E1E100","#EAC100","#F75000"
                    ,"#B87070","#AFAF61","#6FB7B7","#9999CC","#B766AD");
                    var $i = $("<a href='#' style='text-decoration:none'/>").text(a[a_idx]);
                    a_idx = (a_idx + 1) % a.length;
                    var x = e.pageX,
                        y = e.pageY;
                    $i.css({
                        "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
                        "top": y - 20,
                        "left": x,
                        "position": "absolute",
                        "font-weight": "bold",
                        "color": color[Math.floor(Math.random()*color.length)]
                    });
                    $("body").append($i);
                    $i.animate({
                            "top": y - 180,
                            "opacity": 0
                        },
                        1500,
                        function() {
                            $i.remove();
                        });
                });
            });
        </script>
    <script>
        !function() {
            function o(w, v, i) {
                return w.getAttribute(v) || i
            }
            function j(i) {
                return document.getElementsByTagName(i)
            }
            function l() {
                var i = j("script"),
                w = i.length,
                v = i[w - 1];
                return {
                    l: w,
                    z: o(v, "zIndex", -1),
                    o: o(v, "opacity", 0.5),
                    c: o(v, "color", "0,0,0"),
                    n: o(v, "count", 99)
                }
            }
            function k() {
                r = u.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
                n = u.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
            }
            function b() {
                e.clearRect(0, 0, r, n);
                var w = [f].concat(t);
                var x, v, A, B, z, y;
                t.forEach(function(i) {
                    i.x += i.xa,
                    i.y += i.ya,
                    i.xa *= i.x > r || i.x < 0 ? -1 : 1,
                    i.ya *= i.y > n || i.y < 0 ? -1 : 1,
                    e.fillRect(i.x - 0.5, i.y - 0.5, 1, 1);
                    for (v = 0; v < w.length; v++) {
                        x = w[v];
                        if (i !== x && null !== x.x && null !== x.y) {
                            B = i.x - x.x,
                            z = i.y - x.y,
                            y = B * B + z * z;
                            y < x.max && (x === f && y >= x.max / 2 && (i.x -= 0.03 * B, i.y -= 0.03 * z), A = (x.max - y) / x.max, e.beginPath(), e.lineWidth = A / 2, e.strokeStyle = "rgba(" + s.c + "," + (A + 0.2) + ")", e.moveTo(i.x, i.y), e.lineTo(x.x, x.y), e.stroke())
                        }
                    }
                    w.splice(w.indexOf(i), 1)
                }),
                m(b)
            }
            var u = document.createElement("canvas"),
            s = l(),
            c = "c_n" + s.l,
            e = u.getContext("2d"),
            r,
            n,
            m = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
            function(i) {
                window.setTimeout(i, 1000 / 45)
            },
            a = Math.random,
            f = {
                x: null,
                y: null,
                max: 20000
            };
            u.id = c;
            u.style.cssText = "position:fixed;top:0;left:0;z-index:" + s.z + ";opacity:" + s.o;
            j("body")[0].appendChild(u);
            k(),
            window.onresize = k;
            window.onmousemove = function(i) {
                i = i || window.event,
                f.x = i.clientX,
                f.y = i.clientY
            },
            window.onmouseout = function() {
                f.x = null,
                f.y = null
            };
            for (var t = [], p = 0; s.n > p; p++) {
                var h = a() * r,
                g = a() * n,
                q = 2 * a() - 1,
                d = 2 * a() - 1;
                t.push({
                    x: h,
                    y: g,
                    xa: q,
                    ya: d,
                    max: 6000
                })
            }
            setTimeout(function() {
                b()
            }, 100)
    } ();
    </script>
</footer>

        </div>
        
    <script defer src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js"></script>
    <script>
        window.lazyLoadOptions = {
            elements_selector: ".lazy",
            threshold: 0
        };
    </script>
 

 

 

 

 



 



 


    
 


    
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>

    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.4.1/dist/jquery.fancybox.min.css">

    
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.4.1/dist/jquery.fancybox.min.js"></script>

    <script>
        let lazyloadT = Boolean('true'),
            auto_fancybox = Boolean('false')
        if (auto_fancybox) {
            $(".post__content").find('img').each(function () {
                var element = document.createElement("a");
                $(element).attr("data-fancybox", "gallery");
                $(element).attr("href", $(this).attr("src"));
                if (lazyloadT) {
                    $(element).attr("href", $(this).attr("data-srcset"));
                }
                $(this).wrap(element);
            });
        } else {
            $(".post__content").find("fancybox").find('img').each(function () {
                var element = document.createElement("a");
                $(element).attr("data-fancybox", "gallery");
                $(element).attr("href", $(this).attr("src"));
                if (lazyloadT) {
                    $(element).attr("href", $(this).attr("data-srcset"));
                }
                $(this).wrap(element);
            });
        }
    </script>
 

 

 

 

 


    

    
    
    
    <script>
        function loadComment() {
            let e;
            (e = document.createElement("script")).src = 'https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js',
            document.body.appendChild(e);
            e.onload = () => {
                var valineConfig = {"appId":"c21JqSBfScFdueO44Scwp9Ba-gzGzoHsz","appKey":"apETP0QIekHzRXjTHvOnrQmS","placeholder":null,"path":null,"avatar":"retro","meta":["nick","mail","link"],"pageSize":null,"lang":null,"visitor":null,"highlight":null,"avatarForce":null,"recordIP":null,"serverURLs":null,"enableQQ":true,"requiredFields":["nick","mail"],"emojiCDN":null,"emojiMaps":null};
                valineConfig.el = '#valine_container';
                for (var i in valineConfig) {
                    if (valineConfig[i] === null) delete valineConfig[i];
                }
                new Valine(valineConfig);
            };
        }
    
        var runningOnBrowser = typeof window !== "undefined";
        var isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent);
        var supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window;
    
        setTimeout(function () {
            if (!isBot && supportsIntersectionObserver) {
                var comment_observer = new IntersectionObserver(function(entries) {
                    if (entries[0].isIntersecting) {
                        loadComment();
                        comment_observer.disconnect();
                    }
                }, { threshold: [0] });
                comment_observer.observe(document.getElementById('comment'));
            } else {
                loadComment();
            }
        }, 1);
    </script>


    
    
    
    
    

    
    
    
    
    

    
    
    



    </body>
</html>
